from django.shortcuts import render
from rest_framework.generics import ListAPIView
from .serializers import SKUSerializer, SKUIndexSerializer
from .models import SKU
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet

# Create your views here.

class SKUListView(ListAPIView):
    '''sku列表数据'''
    serializer_class = SKUSerializer
    # queryset = SKU.objects.filter(category=???)

    # 排序
    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time', 'price', 'sales')

    # 重写方法
    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id, is_launched=True)

class SKUSearchViewSet(HaystackViewSet):
    serializer_class = SKUIndexSerializer
    index_models = [SKU]